package fs_cmd

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/extension-proto-gen-go.git/extension_fs_api"
	"atomgit.com/openlinksaas/extension-test-cli/utils"
	"github.com/spf13/cobra"
)

var _copyCmd = &cobra.Command{
	Use:   "copy",
	Short: "copy file cross fs",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if len(args) != 4 {
			return fmt.Errorf("miss param:[fromFsId] [fromFileId] [toFsId] [toFileId]")
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		conn, err := utils.ConnServer()
		if err != nil {
			return err
		}
		defer conn.Close()

		client := extension_fs_api.NewExtensionFsApiClient(conn)
		fromFsId := args[0]
		fromFileId := args[1]
		toFsId := args[2]
		toFileId := args[3]

		_, err = client.CopyFile(context.Background(), &extension_fs_api.CopyFileRequest{
			FromFsId:   fromFsId,
			FromFileId: fromFileId,
			ToFsId:     toFsId,
			ToFileId:   toFileId,
		})
		if err != nil {
			return err
		}
		fmt.Printf("copy from %s/%s to %s/%s success\n", fromFsId, fromFileId, toFsId, toFileId)
		return nil
	},
}
